package ch.fusun.baron.player;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import ch.fusun.baron.data.AbstractDataProvider;
import ch.fusun.baron.data.DataUpdate;
import ch.fusun.baron.player.api.Country;
import ch.fusun.baron.player.api.CountryService;
import ch.fusun.baron.player.api.CountryUpdate;

/**
 * Country service
 */
public class CountryServiceImpl extends AbstractDataProvider implements
		CountryService {

	private Map<String, Country> countries = new HashMap<String, Country>();
	private int counter = 0;

	/**
	 * Kryo
	 */
	public CountryServiceImpl() {
	}

	@Override
	public DataUpdate createFullUpdate() {
		synchronized (countries) {
			return new CountryUpdate(countries);
		}
	}

	@Override
	public void setCountries(Map<String, Country> countries) {
		synchronized (countries) {
			this.countries = countries;
			updateAllListeners(new CountryUpdate(countries));
		}
	}

	@Override
	public Country createCountry(String name) {
		synchronized (countries) {
			Country country = new Country(counter++, name);
			countries.put(name, country);
			updateAllListeners(new CreateCountryDataUpdate(name));
			return country;
		}
	}

	@Override
	public Collection<Country> getCountries() {
		synchronized (countries) {
			return countries.values();
		}
	}

}
